
package com.UI;

import com.Model.NotesDbAdapter;


import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.AdapterView.AdapterContextMenuInfo;

/* This class is where the user can see a list of saved notes 
   and tap the Add Notes button to view/edit
*/
public class NotepadAddDelete extends ListActivity {

    private static final int ACTIVITY_EDIT= 1;
    private static final int DELETE_ID = Menu.FIRST + 1;
    private NotesDbAdapter mDbHelper;
    private String cNo;
    private ListView lv1;
    private CourseAdapter c_adapter;
    
    /* Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
    	Bundle lastActivity = getIntent().getExtras();
        cNo = lastActivity.getString("CourseNum");
        
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.notes_list);
    	getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.notes_titlebar);
    	
    	((TextView)findViewById(R.id.titleNotes)).setText("Course Notes " + cNo);
        
    	findViewById(R.id.back).setOnClickListener(new OnClickListener() {
    		public void onClick(View v) {
    			NotepadAddDelete.this.finish();

    		}
    	});
   	
    	findViewById(R.id.plus).setOnClickListener(new OnClickListener() {
    		public void onClick(View v) {
    		//	NotepadAddDelete.this.finish();
       	    			
    			Intent addnotes = new Intent();
    			addnotes.setClassName("com.UI", "com.UI.NotepadEdit");
    			addnotes.putExtra("CourseNum",cNo);
    			startActivity(addnotes);
    			
    		}
    	});
    	    	
        mDbHelper = new NotesDbAdapter(this);
        mDbHelper.open();
        fillData();
        
        lv1= getListView();
        lv1.setTextFilterEnabled(true);      
        registerForContextMenu(getListView());
                                   
    }
    
    // this function will display a list of notes that are saved
    private void fillData() {
    	
    	Bundle lastActivity = getIntent().getExtras();
        cNo = lastActivity.getString("CourseNum");
        Cursor notesCursor = mDbHelper.fetchAllNotes(cNo);
        startManagingCursor(notesCursor);
        
        // Create an array to specify the fields we want to display in the list (only TITLE)
        String[] from = new String[]{NotesDbAdapter.KEY_TITLE};
        
        // and an array of the fields we want to bind those fields to (in this case just text1)
        int[] to = new int[]{R.id.text1};    
        
        // Now create a simple cursor adapter and set it to display
        SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.notes_row, notesCursor, from, to);       
        setListAdapter(notes);
    }

    
    // create an in-built menu item 
    @Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, DELETE_ID, 0, R.string.menu_delete);
	}

    // when in-built menu item (this case delete) is selected we delete note
    @Override
	public boolean onContextItemSelected(MenuItem item) {

		switch(item.getItemId()) {
    	case DELETE_ID:
    		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
	        mDbHelper.deleteNote(info.id,cNo);
	        fillData();
	        return true;
		}
		return super.onContextItemSelected(item);
	}
    
    // this chooses the right row_id of the note that is selected
    //   and activates the onActivityResult method
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);       
        
        Intent i = new Intent(this, NotepadEdit.class);
        i.putExtra("CourseNum",cNo);
        i.putExtra(NotesDbAdapter.KEY_ROWID, id);
        startActivityForResult(i, ACTIVITY_EDIT);
    }
    
    // this activity is called when startActivityForResult is called
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        fillData();
    }
}
